package com.xbongbong.paas.script.controller;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.dto.CustomerCommunicateDeleteDTO;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.saas.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *跟进记录刷新脚本
 *
 *@author weijianxun
 *@date 2022/8/24 10:13
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/customerCommunicate")
public class CustomerCommunicateScriptController {

    private final Logger LOG = LoggerFactory.getLogger(CustomerCommunicateScriptController.class);
    @Resource
    private CustomerService customerService;

    @RequestMapping(value = "/customerRemoveComplete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customerRemoveComplete(@RequestBody @Valid CustomerCommunicateDeleteDTO communicateDeleteDTO, BindingResult br) {
        int start = DateUtil.getInt();
        LOG.info("------------------------- 客户彻底删除跟进记录的数据迁移脚本 -> 开始执行 --------------------------");
        LOG.info("========================= 客户彻底删除跟进记录的数据迁移脚本 执行开始时间：" + start + " ==========================");
        try {
            if (br.hasErrors()) {
                String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
                return errMsg;
            }else {
                communicateDeleteDTO.setDel(DelEnum.REMOVE_COMPLETELY.getDel());
                customerService.customerCommunicateRemoveByScript(communicateDeleteDTO);
            }
        } catch (Exception e) {
            LOG.error("脚本执行失败", e);
            return "fail";
        }
        int time = DateUtil.getInt() - start;
        LOG.info("------------------------- 客户彻底删除跟进记录的数据迁移脚本 -> 执行结束  共计花费{}秒--------------------------", time);
        return "success";
    }
}
